@@ -201,7 +201,15 @@ module LiquidInterpolatable |
||
201 | 201 |
'concat(' << subs.join(', ') << ')' |
202 | 202 |
end |
203 | 203 |
end |
204 |
- |
|
204 |
+ |
|
205 |
+ def regex_replace(input, regex, replacement = ''.freeze) |
|
206 |
+ input.to_s.gsub(Regexp.new(regex), replacement.to_s) |
|
207 |
+ end |
|
208 |
+ |
|
209 |
+ def regex_replace_first(input, regex, replacement = ''.freeze) |
|
210 |
+ input.to_s.sub(Regexp.new(regex), replacement.to_s) |
|
211 |
+ end |
|
212 |
+ |
|
205 | 213 |
private |
206 | 214 |
|
207 | 215 |
def logger |
@@ -177,5 +177,23 @@ describe LiquidInterpolatable::Filters do |
||
177 | 177 |
expect(@agent.interpolated['long_url']).to eq('http://2many.x/6') |
178 | 178 |
end |
179 | 179 |
end |
180 |
+ |
|
181 |
+ describe 'regex replace' do |
|
182 |
+ let(:agent) { Agents::InterpolatableAgent.new(name: "test") } |
|
183 |
+ |
|
184 |
+ it 'should replace the first occurrence of a string using regex' do |
|
185 |
+ agent.interpolation_context['something'] = 'foobar foobar' |
|
186 |
+ agent.options['cleaned'] = '{{ something | regex_replace_first: "\S+bar", "foobaz" }}' |
|
187 |
+ expect(agent.interpolated['cleaned']).to eq('foobaz foobar') |
|
188 |
+ end |
|
189 |
+ |
|
190 |
+ it 'should replace the all occurrences of a string using regex' do |
|
191 |
+ agent.interpolation_context['something'] = 'foobar foobar' |
|
192 |
+ agent.options['cleaned'] = '{{ something | regex_replace: "\S+bar", "foobaz" }}' |
|
193 |
+ expect(agent.interpolated['cleaned']).to eq('foobaz foobaz') |
|
194 |
+ end |
|
195 |
+ |
|
196 |
+ end |
|
197 |
+ |
|
180 | 198 |
end |
181 | 199 |
end |